package com.whu.MenuHub.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.whu.MenuHub.domain.Food;
import com.whu.MenuHub.domain.FoodDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FoodDao extends BaseMapper<Food> {


    @Select("SELECT * FROM food ${ew.customSqlSegment}")
    public IPage<Food> findFoods(IPage <Food> page,
                                    @Param(Constants.WRAPPER) QueryWrapper<Food> wrapper);
    @Select("SELECT * FROM food where foodName=#{foodName}")
    public Food findFoodByName(String foodName);

    @Insert("INSERT INTO food (foodName,photo,price,label) " +
            "VALUES (#{foodName},#{photo},#{price},#{label})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id",
            keyColumn = "id", resultType = Integer.class, before = false)
    void insertFood(Food food);

    //从数据库中查询近7日销量最高的食物
    @Select("select food.id,food.foodName,SUM(foodcomment.foodNumber)as saleNumber " +
            "from foodcomment,food,orders where foodcomment.foodID=food.id and orders.id=foodcomment.orderID " +
            "and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= orders.orderTime group by(food.id) limit 5 ")
    List<Food> getHotFoodWithin7Day();
}
